// pages/profile/index.ts
Page({
  data: {
    userInfo: {
      nickName: '',
      avatar: '',
    },
  },
  async uploadAvatar(e) {
    const res = await wx.http.upload('/upload', {
      name: 'file',
      filePath: e.detail.avatarUrl,
      formData: {
        type: 'avatar',
      },
    })
    this.setData({
      'userInfo.avatar': res.data.url,
    })
  },
  onLoad() {
    this.getUserInfo()
  },
  async getUserInfo() {
    const { data } = await wx.http.get('/userInfo')
    // 演示服务器图片打不开, 给一个默认的，但是如果通过接口上传到oss的例外
    if (!data.avatar.includes('oss')) {
      data.avatar = 'https://img0.baidu.com/it/u=2367160514,329988320&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500'
    }
    this.setData({
      userInfo: data,
    })
  },
  async onusernameChange(e) {
    await wx.http.put('/userInfo', { nickName: e.detail.value })
  },
})
